﻿<%@ Page Title="生产订单对应仓库" Language="C#" MasterPageFile="~/master/m.master" %>

<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
    <link href="../assets/css/shelf/base.css" rel="stylesheet">
    <link href="../assets/css/plugins/webuploader/webuploader.css" rel="stylesheet" />
    <link href="../assets/img/validate/validate.css" rel="stylesheet">
    <link href="../assets/css/plugins/iCheck/customsmall.css" rel="stylesheet" />
    <link href="../assets/css/plugins/stepper/stepper.widget.css" rel="stylesheet" />
    <link href="../assets/css/plugins/jQuery-DataTables/css/dataTables.bootstrap.min.css" rel="stylesheet" />
    <link href="../assets/css/plugins/iCheck/customsmall.css" rel="stylesheet" />
    <style type="text/css">
        .system-box .right-side {
            width: 98%;
        }

        .condition, .condition-title {
            border-bottom: 1px solid #dfdfdd;
        }

        #tbMinChiMa td {
            text-align: center;
        }

            #tbMinChiMa td input {
                text-align: center;
                width: 100%;
            }

        .form-group label {
            max-width: 70px;
            margin-left: 5px;
            padding-left: 1px;
            padding-right: 1px;
        }

        #myTbTr td input {
            padding-left: 2px;
            padding-right: 2px;
        }

        @media (min-width: 750px) {
            .modal-dialog {
                width: 750px;
            }

            .footable {
                border-bottom: 1px solid #dfdfdd;
                border-left: 1px solid #dfdfdd;
            }

                .footable th,
                .footable td {
                    border-top: 1px solid #dfdfdd;
                    border-right: 1px solid #dfdfdd;
                    text-align: center;
                }

            .tb-head-container {
                padding-right: 6px;
                background: #fff;
            }

            .tb-body-container {
                overflow-x: hidden;
                padding-right: 0px;
                background: #fff;
            }
            /*所有表格行高缩小*/
            .table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th {
                padding: 1px !important;
            }
        }
    </style>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" runat="Server">
    <div class="system-box">
        <div class="right-side">
            <div class="btns-container">
                <div class="modal inmodal" id="addWindow" tabindex="-1" role="dialog" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content animated bounceInRight">
                            <div class="modal-header">
                                <span class="editTitle"></span>
                                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span> <span class="sr-only">关闭</span> </button>
                            </div>
                            <div id="message" style="display: none"></div>
                            <div class="modal-body clearfix">
                                <form class="form-horizontal">
                                    <div class="form-group">
                                        <label for="CompanyId" class="col-sm-2 control-label">公司编码</label>
                                        <div class="col-sm-10">
                                            <input type="text" id="CompanyId" name="CompanyId" placeholder="" class="form-control" readonly="readonly" value="" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="BillNo" class="col-sm-2 control-label">生产订单</label>
                                        <div class="col-sm-10">
                                            <input type="text" id="BillNo" name="BillNo" placeholder="请输入生产订单" datacol="yes" err="请输入生产订单" checkexpession="NotNull" class="form-control required" />
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label for="Warehouse" class="col-sm-2 control-label">仓库</label>
                                        <div class="col-sm-10">
                                            <table id="Warehouse" class=" table table-bordered " width="100%" style="margin-top: 0 !important; border: 0; border-bottom: 1px solid #ebebeb;">
                                            </table>
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <div class="modal-footer">
                                <button type="button" id="addModuleBtn" class="btn btn-primary">保存</button>
                                <button type="button" id="closeAddBox" class="btn btn-white" data-dismiss="modal">关闭</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="condition">
                <label>仓库</label>
                <select class="form-control  input-w170" id="search-stockID-step3">
                    <option value="">请选择</option>
                </select>
                <label>生产订单</label>
                <input id="search-billNo" class="form-control input-w170" placeholder="输入生产订单" />
            </div>

            <div class="right-content-box">
                <div class="ibox-content">
                    <table class="footable table table-stripped">
                        <thead></thead>
                        <tbody id="pageList"></tbody>
                    </table>
                </div>
            </div>

            <div class="pageControl"></div>
        </div>
    </div>

</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" runat="Server">
    <script src="../assets/js/jquery-ui-1.10.4.min.js"></script>
    <script src="../assets/img/validate/learunui-validator.js"></script>
    <script src="../assets/js/page/pageControl.js"></script>
    <script src="../assets/js/page/rScroll.js"></script>
    <script src="../assets/js/page/tableControl.js"></script>
    <script src="../assets/js/plugins/webuploader/webuploader.min.js"></script>
    <script src="../assets/js/framework.js"></script>
    <script src="../assets/js/plugins/bootstrap-typeahead/bootstrap-typeahead.js"></script>
    <script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
    <script src="../assets/js/plugins/iCheck/icheckAll.js"></script>
    <script src="../assets/js/plugins/stepper/stepper.widget.js"></script>
    <script src="../assets/js/plugins/linq/jquery.linq.min.js"></script>
    <script src="../assets/js/plugins/jQuery-DataTables/jquery.dataTables.min.js"></script>
    <script src="../assets/js/plugins/jQuery-DataTables/dataTables.bootstrap.js"></script>
    <script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
    <script src="../assets/js/plugins/iCheck/icheckAll.js"></script>
    <script>
        $('document').ready(function () {
            //模块ID
            var ModuleID = Y.getUrlParam("ModuleID");
            //当前页数
            var page = 1;
            commonUtil.loadBtnList(ModuleID, function () {
                $('#editBtn').attr("disabled", "disabled");
                $('#deleteBtn').attr("disabled", "disabled");
                rScroll();
            });

            //绑定
            if ($("#search-stockID-step3 option").length <= 1) {
                $("#search-stockID-step3").html('<option value="">请选择</option>');
                commonUtil.bindOdsErpBasStocks2({}, ['#search-stockID-step3']); //配货仓库
            }

            var dataList = [];

            var CompanyId;
            var BillNo;

            IsCreate = true;
            //虚拟仓选择控件
            var Warehouse;
            var WarehouseHeight = 260;
            //处理表头
            if ($('.footable thead').children().size() == 0) {
                tableControl.initThead('.footable',
                [
                    {
                        trClass: '',
                        children: [
                            { tdText: '', c: 0, r: 0, tdHtml: '<input type="checkbox" id="checkAllPlan" />', tdId: '', tdClass: 'hide diy-td-w30' },
                            { tdText: '公司代码', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'diy-td-w50' },
                            { tdText: '生产订单', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'diy-td-w100' },
                            { tdText: '仓库', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'diy-td-w150' },
                        ]
                    }
                ]);
            }

            var loadWarehouse = function (a, b, callback) {
                Warehouse = $('#Warehouse').DataTable({
                    ajax: {
                        url: "/api.ashx?c=OdsErpBasArea&a=GetAllOdsErpBasStockList",
                        type: "POST",
                        dataSrc: function (json) {
                            if (json.Success == false) {
                                swal(json.Message, '', 'error');
                                return [];
                            } else {
                                //把广东仓的删除 特殊处理
                                $.Enumerable.From(json.Data).ForEach(function (x, i) {
                                    if (x.StockName == '广东仓') {
                                        json.Data.splice(i, 1);
                                    }

                                });
                                var m = json.Data.length % 3 == 0;
                                var _leng = m ? json.Data.length : parseInt(json.Data.length / 3) + 1;
                                json.data = [];
                                var skip = -1;
                                $.map(json.Data, function (obj, i) {
                                    if ((i + 1) % 3 == 1) {
                                        skip++;
                                        json.data.push({
                                            StockCode: json.Data[i].StockCode, StockID: json.Data[i].StockID, StockName: json.Data[i].StockName
                                            , StockCode2: "", StockID2: "", StockName2: "",
                                            StockCode3: "", StockID3: "", StockName3: ""
                                        })

                                    }
                                    if ((i + 1) % 3 == 2) {

                                        json.data[skip]["StockCode2"] = json.Data[i].StockCode;
                                        json.data[skip]["StockID2"] = json.Data[i].StockID;
                                        json.data[skip]["StockName2"] = json.Data[i].StockName;
                                    }
                                    if ((i + 1) % 3 == 0) {
                                        json.data[skip]["StockCode3"] = json.Data[i].StockCode;
                                        json.data[skip]["StockID3"] = json.Data[i].StockID;
                                        json.data[skip]["StockName3"] = json.Data[i].StockName;
                                    }

                                });
                                json.data.splice(_leng, json.Data.length);
                                return json.data;
                            }
                        }
                    },
                    scrollY: WarehouseHeight,
                    scrollCollapse: true,
                    retrieve: true,
                    language: { url: "../assets/js/plugins/jQuery-DataTables/i18n/Chinese.json" }, scrollX: true, searching: false, paging: false, info: false, ordering: false, lengthChange: false, processing: true,
                    drawCallback: function (setting) {
                        console.log("hi");
                        var api = this.api();
                        $(Warehouse.table().container()).find(".checkItem").iCheck({ checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green", });
                        Warehouse.$("input.checkItem").off("ifClicked");
                        Warehouse.$("input.checkItem").on("ifClicked", function () {
                        });
                        if (callback != undefined) {
                            callback(a, b);
                        }
                    },
                    columns: [
                     {
                         data: "StockID", Sortable: false, targets: 0,
                         title: "", className: "text-center",
                         render: function (data, type, row, meta) {
                             if (Y.checkNull(row.StockID))
                                 return "";
                             else
                                 return '<input type="checkbox" name="checkItemstep22" class="checkItem" data-text="' + row.StockName + '" value="' + row.StockID + '" />';
                         },
                     },
                    { title: "虚拟仓", data: "StockName", className: "text-center", width: "27%" },
                     {
                         data: "StockID2", Sortable: false, targets: 0,
                         title: "", className: "text-center",
                         render: function (data, type, row, meta) {
                             if (Y.checkNull(row.StockID2))
                                 return "";
                             else
                                 return '<input type="checkbox" name="checkItem" class="checkItem" data-text="' + row.StockName2 + '"  value="' + row.StockID2 + '" />';
                         },
                     },
                    { title: "虚拟仓", data: "StockName2", className: "text-center", width: "27%" },
                    {
                        data: "StockID3", Sortable: false, targets: 0,
                        title: "", className: "text-center",
                        render: function (data, type, row, meta) {
                            if (Y.checkNull(row.StockID3))
                                return "";
                            else
                                return '<input type="checkbox" name="checkItemstep22" class="checkItem" data-text="' + row.StockName3 + '" value="' + row.StockID3 + '" />';
                        },
                    },
                    { title: "虚拟仓", data: "StockName3", className: "text-center", width: "27%" },

                    ]
                });
                if (callback != undefined) {
                    callback(a, b);
                }
            }

            var pageList = [];
            //获取列表
            var loadList = function () {
                $('#editBtn').attr('disabled', 'disabled');
                $('#deleteBtn').attr('disabled', 'disabled');

                shopCode = '';
                materialID = '';
                sizeID = '';

                pageControl.init('ProductionOrderWarehouse', 'GetList', {
                    CompanyId: 'KM',
                    Warehouse: $('#search-stockID-step3').val(),
                    BillNo: $('#search-billNo').val(),
                    Page: page,
                    Size: 50,
                    needLayer: true,
                    alwaysCallBack: function () {
                        $('#pageList').empty();
                    }
                }, function (result) {
                    pageList = [];
                    page = result.page;
                    var tBody = [];
                    if (!Y.checkNull(result.data)) {
                        if (result.data.Items.length > 0) {
                            dataList = result.data.Items;
                            var IsEmpty = '0';
                            result.data.Items.map(function (items, i) {
                                var tempChild = [];
                                tempChild.push({
                                    tdText: '',
                                    c: 0,
                                    r: 0,
                                    tdHtml:
                                        '<input type="checkbox" class="checkPlan" data-index="' +
                                            i +
                                            '" />',
                                    tdId: '',
                                    tdClass: 'hide'
                                });
                                tempChild.push({
                                    tdText: '',
                                    c: 0,
                                    r: 0,
                                    tdHtml: '<span style="display:inline-block; width:100px;">' +
                                        (items[0].CompanyID || IsEmpty) +
                                        '</span>',
                                    tdId: '',
                                    tdClass: 'CompanyID'
                                });
                                tempChild.push({
                                    tdText: '',
                                    c: 0,
                                    r: 0,
                                    tdHtml: '<span style="display:inline-block; width:150px;">' + (items[0].BillNo || IsEmpty) + '</span>',
                                    tdId: '',
                                    tdClass: 'BillNo'
                                });
                                tempChild.push({
                                    tdText: '',
                                    c: 0,
                                    r: 0,
                                    tdHtml: '<span style="display:inline-block; width:100%;">' + (items[0].StockName || IsEmpty) + '</span>',
                                    tdId: '',
                                    tdClass: 'StockName'
                                });
                                var tempTr = {
                                    trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd') + ' ' + items[0].BillNo,
                                    children: tempChild
                                };
                                tBody.push(tempTr);
                            });
                        }
                        rScroll();
                    }
                    tableControl.initTbody('.footable', (tBody.length > 0 ? tBody : null), 13, null);
                    //选择全部插件
                    $(".footable").icheckAll({
                        checkObj: ".checkPlan",//表格下面的选择控件对象 选择器
                        checkAllObj: "#checkAllPlan",//全选 选择器
                        clickoutBoxCheck: false,//点击行 不选中
                        checkboxMargin: "4px 0 0 0 "//上右下左
                    });
                });
            }

            var resetInput = function () {
                var data = sessionStorage.getItem('login-info');
                var d = $.parseJSON(data);
                if (!Y.checkNull(d.CompanyID))
                    $('#CompanyId').val(d.CompanyID);
                $("#BillNo").val("");
                $("#Warehouse div").removeClass("checked");
                $("#Warehouse input").removeClass("checked");
                $("#Warehouse tr").removeClass("checked");
                $("#Warehouse td").removeClass("checked");
            };


            //选中记录
            $('#pageList').on('click',
                'tr',
                function () {
                    CompanyId = $(this).find('.CompanyID').text();
                    BillNo = $(this).find('.BillNo').text();
                    if (!Y.checkNull(CompanyId) && !Y.checkNull(BillNo)) {
                        $('#editBtn').removeAttr('disabled');
                        $('#deleteBtn').removeAttr('disabled');
                    }

                    $('.footable tr').removeClass('check');

                    var trClass = $(this).attr("class");
                    trClass = trClass.replace(/gradeX /, "");
                    trClass = trClass.replace(/footable-even /, "");
                    trClass = trClass.replace(/footable-odd /, "");
                    trClass = '.' + trClass;
                    $(trClass).addClass("check");

                });

            //查询按钮
            $('.btns-container').on('click', '#btn-search', function () {
                page = 1;
                loadList();
            });

            //新增按钮
            $('.btns-container').on('click', '#addBtn', function () {
                $('.editTitle').text('新增生产订单对应仓库');
                $('#addModuleBtn').show();
                IsCreate = true;
                resetInput();
                loadWarehouse();
            });

            //编辑按钮
            $('.btns-container').on('click', '#editBtn', function () {

                $('.editTitle').text('编辑生产订单对应仓库');
                $('#addModuleBtn').show();
                resetInput();
                IsCreate = false;
                loadWarehouse(CompanyId, BillNo, loadDetail);
            });

            //删除按钮
            commonUtil.initDeleteBtnEvent({
                callBack: function () {
                    //debugger;
                    if (CompanyId == "" || BillNo == "") {
                        swal("请选择要删除的项", '', 'error');
                        return false;
                    }
                    Y.API2('ProductionOrderWarehouse', 'Remove',
                    {
                        companyId: CompanyId,
                        billNo: BillNo
                    }
                        , function (data) {
                            layer.closeAll('dialog');
                            if (data.Success == false) {
                                swal('删除失败', data.Message, 'error');
                            } else {
                                swal('删除成功', '该信息已删除', 'success');
                                loadList();
                            }
                        });
                }
            });


            //保存
            $('#addModuleBtn').click(function () {
                if (!CheckDataValid('.form-horizontal')) {
                    return false;
                }

                var stock = [];
                Warehouse.$(".checkItem:checked").each(function () {
                    console.log($(this));

                    console.log($(this).val());
                    stock.push($(this).val());
                });
                console.log("stock=" + stock);

                if (Y.checkNull(stock)) {
                    swal("请选择虚拟仓！");
                    return;
                }
                var BillNo = $("#BillNo").val();
                if (Y.checkNull(BillNo)) {
                    swal("请输入生产订单！");
                    return;
                }

                var action = "";
                var successText = "";
                if (IsCreate) {
                    action = "Insert";
                    successText = '新增成功';
                }
                else {
                    action = "Update";
                    successText = '编辑成功';
                }

                layer.msg('玩命加载中……', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });
                Y.API2('ProductionOrderWarehouse', action, {
                    CompanyId: $('#CompanyId').val(),
                    BillNo: $('#BillNo').val(),
                    WarehouseList: stock
                }, function (data) {
                    layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        swal(successText, '', 'success');
                        //关闭新建  or 编辑框
                        $('#closeAddBox').click();
                        //刷新列表
                        loadList();
                    }
                });
            });

            //导入
            $(".btns-container").on("click", '#importBtn', function () {
                $.ImportExcelTemplate({
                    filetype: ['PRODUCTIONORDERWAREHOUSE'],
                    filename: [
                        { name: '生产订单对应仓库', state: null }
                    ],
                    action: 'ImportTemplate',
                    after: function () {
                        //回调方法
                        loadList();
                    }
                });
            });

            loadList();
            //loadWarehouse();
            //获取明细
            var loadDetail = function (companyId, billNo) {
                if (CompanyId == "" || BillNo == "") {
                    swal("请选择要修改的项", '', 'error');
                    return false;
                }
                layer.msg('玩命加载中……', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });
                Y.API2('ProductionOrderWarehouse', 'GetDetails',
                    {
                        companyId: companyId,
                        billNo: billNo
                    }
                    , function (data) {
                        layer.closeAll('dialog');
                        if (data.Success == false) {
                            swal(data.Message, '', 'error');
                        } else {
                            if (!Y.checkNull(data)) {
                                if (data.length > 0) {
                                    data.map(function (item, i) {
                                        if (i == 0) {
                                            $('#CompanyID').val(item.CompanyID);
                                            $('#BillNo').val(item.BillNo);
                                        }
                                        // $('#Warehouse [value=' + item.StockId + ']').addClass("checked")
                                        $('#Warehouse [value=' + item.StockId + ']').parents().addClass("checked")
                                    })
                                }
                            }
                        }
                    });
            }
        });




    </script>
</asp:Content>
